package com.zhoug.common.utils.encrypt;

import java.security.NoSuchAlgorithmException;
import java.util.UUID;

import javax.crypto.SecretKey;

/**
 * 密码生成器
 * @Author 35574
 * @Date 2021/3/17
 * @Description
 */
public class KeyGenerator {

    /**
     * 自动生成密匙
     * @param keyBitSize 密匙位数,128,192,256位 即16,24,32个字节
     * @return 生成的密匙字节数组
     */
    public static byte[] generateKey(int keyBitSize, String algorithm) {
        javax.crypto.KeyGenerator keyGenerator = null;
        try {
            keyGenerator = javax.crypto.KeyGenerator.getInstance(algorithm);
            keyGenerator.init(keyBitSize);
            SecretKey secretKey = keyGenerator.generateKey();
            return secretKey.getEncoded();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }


        /**
         * 随机生成32位的key
         *
         * @return
         */
    public static String generate32Key() {
        //每次启动app动态生成key
        return UUID.randomUUID().toString().replace("-", "");
    }

}
